package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetRadioAnchorListBean;
import com.rjwh.dingdong.client.bean.localbean.GetRadioAnchorListBean;
import com.rjwh.dingdong.client.bean.localbean.RadioAnchorListData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.RadioAnchorListModel;
import com.rjwh.dingdong.client.mvp.presenter.IRadioAnchorListPresenter;
import com.rjwh.dingdong.client.mvp.view.RadioAnchorListView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName RadioAnchorListPresenter
 * @Description
 * @date 2016-11-17
 * @history 2016-11-17 author: description:
 */

public class RadioAnchorListPresenter extends BasePresenter<RadioAnchorListView> implements IRadioAnchorListPresenter {
    private RadioAnchorListModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public RadioAnchorListPresenter(Context context, RadioAnchorListView mMvpView) {
        super(context, mMvpView);
        model = new RadioAnchorListModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            switch (businessType) {
                case NetConstant.GETRADIOLIST:
                    getMvpView().setRefreshing(false);
                    ResGetRadioAnchorListBean resGetRadioAnchorListBean = (ResGetRadioAnchorListBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetRadioAnchorListBean != null) {
                        GetRadioAnchorListBean data = resGetRadioAnchorListBean.getData();
                        if (data != null) {
                            List<RadioAnchorListData> radiolist = data.getRadiolist();
                            if (radiolist != null) {
                                if (radiolist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, radiolist);
                            }
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

    /**
     * 获取主播列表(主播电台、创客频道)
     *
     * @param category  分类owner|creater
     * @param pageindex 页数
     * @param isRefresh
     * @param direction
     */
    @Override
    public void doGetRadioList(String category, int pageindex, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetRadioList(category, pageindex, isRefresh);
    }
}
